package models;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

import play.data.validation.Required;
import play.db.jpa.GenericModel;

@Entity
public class Formation extends GenericModel {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "FORMATION_ID")
	private int formationID;

	@Enumerated(EnumType.ORDINAL)
	@Column(name = "FORMATION_TYPE_ID")
	private FormationType type;

	@Required
	@Column(name = "title")
	private String title;

	@Required
	@Column(name = "begin_date")
	private Date startDate;

	@Required
	@Column(name = "end_date")
	private Date endDate;

	@Lob
	@Column(name = "establishment")
	private String establishment;

	@ManyToMany
	@JoinTable(name = "FormationSkills", joinColumns = { @JoinColumn(name = "FORMATION_ID", referencedColumnName = "FORMATION_ID") }, inverseJoinColumns = { @JoinColumn(name = "SKILL_ID", referencedColumnName = "SKILL_ID") })
	private Set<Skill> formationSkills;

	public Formation() {
		super();
	}

	public Formation(String title, Date startDate, Date endDate,
			String establishment) {
		super();
		this.formationSkills = new HashSet<Skill>();
		this.title = title;
		this.startDate = startDate;
		this.endDate = endDate;
		this.establishment = establishment;
	}

	public int getFormationID() {
		return formationID;
	}

	public void setFormationID(int formationID) {
		this.formationID = formationID;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public FormationType getType() {
		return type;
	}

	public void setType(FormationType type) {
		this.type = type;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public String getEstablishment() {
		return establishment;
	}

	public void setEstablishment(String establishment) {
		this.establishment = establishment;
	}

	public Set<Skill> getFormationSkills() {
		return formationSkills;
	}

	public void setFormationSkills(Set<Skill> formationSkills) {
		this.formationSkills = formationSkills;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((endDate == null) ? 0 : endDate.hashCode());
		result = prime * result
				+ ((establishment == null) ? 0 : establishment.hashCode());
		result = prime * result + formationID;
		result = prime * result
				+ ((formationSkills == null) ? 0 : formationSkills.hashCode());
		result = prime * result
				+ ((startDate == null) ? 0 : startDate.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Formation other = (Formation) obj;
		if (endDate == null) {
			if (other.endDate != null)
				return false;
		} else if (!endDate.equals(other.endDate))
			return false;
		if (establishment == null) {
			if (other.establishment != null)
				return false;
		} else if (!establishment.equals(other.establishment))
			return false;
		if (formationID != other.formationID)
			return false;
		if (formationSkills == null) {
			if (other.formationSkills != null)
				return false;
		} else if (!formationSkills.equals(other.formationSkills))
			return false;
		if (startDate == null) {
			if (other.startDate != null)
				return false;
		} else if (!startDate.equals(other.startDate))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		if (type != other.type)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Formation [formationID=" + formationID + ", title=" + title
				+ ", startDate=" + startDate + ", endDate=" + endDate
				+ ", establishment=" + establishment + ", type=" + type
				+ ", formationSkills=" + formationSkills + "]";
	}

}
